﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using Crm.CrmClient;

namespace Crm.Views
{
    public partial class RaporYap : ChildWindow
    {
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();
        public RaporYap()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RaporYap_Loaded);
            this.cmbKayitliRapor.SelectionChanged += new SelectionChangedEventHandler(cmbKayitliRapor_SelectionChanged);
            client.RaporEkleCompleted += client_RaporEkleCompleted;
            client.RaporSilCompleted += client_RaporSilCompleted;
            client.RaporListeleCompleted += client_RaporListeleCompleted;
        }

        void client_RaporListeleCompleted(object sender, RaporListeleCompletedEventArgs e)
        {
            if (e.Error ==null && e.Result !=null)
            {
                cmbKayitliRapor.ItemsSource = e.Result;
                cmbKayitliRapor.DisplayMemberPath = "Adi";
            }
        }

        void client_RaporSilCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Silindi !");
            }
        }

        void client_RaporEkleCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error ==null)
            {
                MessageBox.Show("Kaydedildi !");
            }
        }

        void cmbKayitliRapor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbKayitliRapor.SelectedIndex > -1)
            {
                Rapor rpr = ((Rapor)cmbKayitliRapor.SelectedItem);
                txtGrafikAlanAdi.Text = rpr.ChartAlan;
                txtGrafikDegerKolon.Text = rpr.ChartDeger;
                txtGrafikSql.Text = rpr.ChartSorgu;
                txtRaporAdi.Text = rpr.Adi;
                txtRaporSql.Text = rpr.GridSorgu;
                cmbGrafikTipi.SelectedIndex = rpr.ChartTipi;
                cmbRaporTipi.SelectedIndex = rpr.Tipi;
            }
        }

        void RaporYap_Loaded(object sender, RoutedEventArgs e)
        {
            client.RaporListeleAsync();
        }

        private void SaveRapor()
        {
            bool devam = false;
            if (cmbRaporTipi.SelectedIndex == 0)
            {
                if (!string.IsNullOrEmpty(txtRaporAdi.Text) && !string.IsNullOrEmpty(txtRaporSql.Text))
                    devam = true;
            }
            else
            {
                if (!string.IsNullOrEmpty(txtRaporAdi.Text) && !string.IsNullOrEmpty(txtRaporSql.Text) && cmbGrafikTipi.SelectedIndex > -1 && !string.IsNullOrEmpty(txtGrafikSql.Text) && !string.IsNullOrEmpty(txtGrafikDegerKolon.Text) && !string.IsNullOrEmpty(txtGrafikAlanAdi.Text))
                    devam = true;
            }
            
            if (devam)
            {
                Rapor newrpr = null;
                if (cmbKayitliRapor.SelectedIndex > -1)
                {
                    newrpr = ((Rapor)cmbKayitliRapor.SelectedItem);
                }
                else
                {
                    newrpr = new Rapor();
                }

                newrpr.Adi = txtRaporAdi.Text;
                newrpr.ChartAlan = (txtGrafikAlanAdi.Text == string.Empty) ? "0" : txtGrafikAlanAdi.Text;
                newrpr.ChartDeger = (txtGrafikDegerKolon.Text == string.Empty) ? "0" : txtGrafikDegerKolon.Text;
                newrpr.ChartSorgu = (txtGrafikSql.Text == string.Empty) ? "0" : txtGrafikSql.Text;
                newrpr.ChartTipi = (cmbGrafikTipi.SelectedIndex == -1) ? byte.MinValue : (byte)cmbGrafikTipi.SelectedIndex;
                newrpr.GridSorgu = txtRaporSql.Text;
                newrpr.Tipi = (byte)cmbRaporTipi.SelectedIndex;
                client.RaporEkleAsync(newrpr);
            }
        }

        private void DeleteRapor()
        {
            if (cmbKayitliRapor.SelectedIndex > -1)
            {
                Rapor newrpr = ((Rapor)cmbKayitliRapor.SelectedItem);
                client.RaporSilAsync(newrpr);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SaveRapor();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteRapor();
        }
    }
}

